package com.dayu.springbootai.entity.vo;

import lombok.NoArgsConstructor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.MessageType;

import java.io.Serializable;

/**
 * 消息VO类
 * 用于封装聊天消息的信息，包括角色和内容
 * 作为Controller层与前端交互的数据传输对象(DTO)
 * 
 * @author dayu
 * @date 2025/4/9 23:28
 */
@NoArgsConstructor
public class MessageVO implements Serializable {
    private static final long serialVersionUID = 1L;
    
    /**
     * 消息发送者的角色
     * 可能的值：user(用户)、system(系统)、assistant(助手)、function(函数)
     */
    private String role;
    
    /**
     * 消息内容
     */
    private String content;

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 构造函数：将Spring AI的Message对象转换为MessageVO对象
     * 
     * @param msg Spring AI的Message对象
     */
    public MessageVO(Message msg) {
        // 获取消息类型
        MessageType messageType = msg.getMessageType();
        
        // 根据消息类型设置角色
        switch (messageType){
            case USER:
                role = "user";
                break;
            case SYSTEM:
                role = "system";
                break;
            case ASSISTANT:
                role = "assistant";
                break;
            case TOOL:
                role = "function";
                break;
            default:
                role = "unknown";
                break;
        }
        
        // 设置消息内容
        this.content = msg.getText();
    }
}